############################################
# Master R file 

# Preliminaries
rm(list = ls())

options(scipen = 999, stringsAsFactors = FALSE)

memory.limit(50000)

############################################
# Define directories 
# Root: manually set to personal working directory
root <- "/Users/tbrai/Dropbox/DBT/Data/ePOS RCT Jharkhand/ABBA replication package_20200814"

DataDir       <- paste0(root, "/Data/")
AdminDataDir  <- paste0(root, "/Data/Admin/")
SurveyDataDir <- paste0(root, "/Data/Survey/")
OutputDir     <- paste0(here::here(), "/replication/exhibits/")
SourceDir     <- paste0(here::here(), "/replication/code/")

############################################
# Install and load packages

packages <- c("rgdal", "rgeos", "gpclib", "ggplot2",
              "foreign", "readstata13", "plyr", "dplyr",
              "RColorBrewer", "scales", "starpolishr", 
              "stargazer", "xlsx", "maptools", "colorspace", 
              "ggpubr", "crayon", "plm", "reshape2", 
              "miceadds", "biostat3")

package_check <- 
  lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, dependencies = TRUE)
      library(x, character.only = TRUE)
    }
  }
)

# Time replication process
start_time <- Sys.time()

############################################
# Figure 2: Value received as a proportion of entitlement
# This figure plots the empirical cumulative distribution, separately for households in treatment and control blocks, of value received divided by 
# value entitled per month, pooling the endline one months
source(paste0(SourceDir, "Figure2.R"))

############################################
# Figure3_PanelA: Effects of reconciliation on value disbursed
# This figure plots the evolution of the average value of commodities disbursed from January to November of 2017
source(paste0(SourceDir, "Figure3_PanelA.R"))

############################################
# Figure3_PanelB: Effects of reconciliation on value received
# This figure plots the evolution of the average value of commodities rceived from January to November of 2017
source(paste0(SourceDir, "Figure3_PanelB.R"))

############################################
# Table A_1: Representativeness within Jharkhand
# This table compares the 10 districts studied with the remaining 14 districts
source(paste0(SourceDir, "TableA_1.R"))

############################################
# Table A_13: Heterogeneous effect by subjective FPS rating
# This table checks the heterogeneous effect of treatment by a dimension of implementation quality
source(paste0(SourceDir, "TableA_13.R"))

############################################
# Figure A_1: Blockwise treatment assignment
# This figure shows the assignment of districts within Jharkhand to study (10) and non-study (14) status, and the assignment of blocks within these 
# districts to treatment and control
source(paste0(SourceDir, "FigureA_1.R"))

############################################
# FigureA_4_PanelA: Effects of reconciliation on value disbursed by treatment
# This figure plots the evolution of the average value of commodities disbursed from January to November of 2017 by treatment and control
source(paste0(SourceDir, "FigureA_4_PanelA.R"))

############################################
# FigureA_4_PanelB: Effects of reconciliation on value received by treatment
# This figure plots the evolution of the average value of commodities received from January to November of 2017 by treatment and control
source(paste0(SourceDir, "FigureA_4_PanelB.R"))

############################################
# Table B_7: Spillover effect of ABBA on allotment
# This table reports  the impact of ABBA on government allotment by block
source(paste0(SourceDir, "TableB_7.R"))

############################################
# Figure D.1: Adherence of disbursement and stock to reconciliation policy, by month
# This figure shows the scatterplots of the left- and right-hand sides of Equations 1 and 2 in draft for the months July-October 2017 using aggregate 
# measures obtained from NIC data in kilograms
source(paste0(SourceDir, "FigureD_1.R"))

############################################
# Figure D.2: Comparison of offtake from three data sources, by month
# This figure compares offtake from three data sources: NIC aggregates provided by the government, transaction data from ePOS records, and survey 
# data from beneficiaries in kilograms
source(paste0(SourceDir, "FigureD_2.R"))

end_time <- Sys.time()
end_time - start_time
